import React from "react";
import { Link, Routes, Route, useParams, useLocation } from "react-router-dom";

const Home = () => {
  return <div>这是首页</div>;
};
const About = () => {
  const location = useLocation();
  console.log(location);
  return <div>这是关于页</div>;
};
const Detail = () => {
  // 如果要拿路由信息的，那么可以使用useParams
  const params = useParams();
  console.log(params);

  return <div>详情页 - {params.id}</div>;
};

const App = () => {
  return (
    <>
      <h2>router-v6-params</h2>
      <ul>
        <li>
          <Link to="/home">首页</Link>
        </li>
        <li>
          {/* <Link to="/about">关于页</Link> */}
          {/* 6版本的state要写在和to并列的地方，而不是to的里面 */}
          <Link
            to={{
              pathname: "/about",
              search: "?a=3",
            }}
            state={{ x: 10 }}
          >
            关于页
          </Link>
        </li>
        <li>
          <Link to="/detail/134">这是详情页</Link>
        </li>
      </ul>

      <hr />

      <Routes>
        <Route path="/home" element={<Home />}></Route>
        <Route path="/about" element={<About />}></Route>
        {/* 动态路由的写法和之前一样，使用/:id */}
        {/* element的写法就是没有路由信息的 */}
        {/* <Route path="/detail/:id" element={<Detail />}></Route> */}

        {/* 动态可选路由 */}
        <Route path="/detail" element={<Detail />}>
          <Route path=":id" element={<Detail />}></Route>
        </Route>
      </Routes>
    </>
  );
};

export default App;
